<?php
defined('BASEPATH') OR exit('No direct script access allowed');
ini_set('date.timezone','Asia/Shanghai');
require_once APPPATH . "third_party/wxpay/lib/WxPay.Api.php";
require_once APPPATH . "third_party/wxpay/WxPay.JsApiPay.php";
require_once APPPATH . "third_party/wxpay/notify.php";
/**
 * Created by PhpStorm.
 * User: chenjz
 * Date: 2017/8/10
 * Time: 14:33
 */
class Wx_pay
{
    /** @var CI_Controller $CI */
    private $CI;

    public function __construct()
    {
        $this->CI = &get_instance();
    }

    /**
     * 生成订单信息
     * @param $openid
     * @return array()
     */
    public function getOrder($openid, $total_fee){
        //统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody('祝福盒子充值款');
        $input->SetAttach("zfhz");
        $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("zfhz");
        $input->SetNotify_url("https://zfhz.7dar.com/wxapp-zfhz/index.php/weixin/notify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = WxPayApi::unifiedOrder($input);
        $order['out_trade_no'] = $input->GetOut_trade_no();//商户订单号也加到订单数据里
        return $order;
    }

    /**
     * 根据订单获取调用支付接口所用的参数
     * @return json数据
     */
    public function getJsApiParameters($order){
        $tools = new JsApiPay();
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

    /**
     * 二次确认订单
     */
    public function notify(){
        $notify = new PayNotifyCallBack();
        $notify->Handle(false);
    }
}